﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAcquisitionBLL
{
    public class Tool
    {
        /// <summary>十六进制字符串转byte
        /// 
        /// </summary>
        /// <param name="hs"></param>
        /// <returns></returns>
        public static byte[] HexStringToBytes(string hs)
        {
            string a = hs.Replace(" ", "");
            int bytelength = 0;
            if (a.Length % 2 == 0)
            {
                bytelength = a.Length / 2;
            }
            else
            {
                bytelength = a.Length / 2 + 1;
            }
            byte[] b = new byte[bytelength];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < bytelength; i++)
            {
                if (i == bytelength - 1)
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2), 16);
                }
                else
                {
                    b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
                }
            }
            //按照指定编码将字节数组变为字符串
            return b;
        }

        /// <summary>字符串转GB2312
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetHexFromChs(string s)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(s);
            StringBuilder Text = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                Text.Append(string.Format("{0:X}", bytes[i]));
                //Text.Append(" ");
            }

            string ggg = Text.ToString();
            string ggf = ggg.Substring(ggg.Length - 1, 1);
            if (ggf == " ")
            {
                ggg = ggg.Remove(ggg.Length - 1, 1);
            }
            return ggg;
        }

        /// <summary>
        /// 秒->时:分:秒
        /// </summary>
        /// <param name="duration"></param>
        /// <returns></returns>
        public static string SecondToHMS(long duration)
        {
            TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));
            string str = "";
            if (ts.Hours > 0)
            {
                str = String.Format("{0:00}", ts.Hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes > 0)
            {
                str = "00:" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes == 0)
            {
                str = "00:00:" + String.Format("{0:00}", ts.Seconds);
            }
            return str;
        }

        /// <summary>转换ushort
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Int16 ConvertUshort(ushort data)
        {
            UInt16 inputU16 = Convert.ToUInt16(data.ToString());
            return (Int16)(-1 & inputU16);
        }
    }
}
